package com.newtype.meeting.client.widget.shared;

import java.util.Map;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.CheckChangedEvent;
import com.extjs.gxt.ui.client.event.CheckChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;

/**
 * 部门可多选窗口
 * @author 汪恭泽
 * Jun 17, 2011 1:24:06 PM
 */
public class DeptTreeMultiSelectWindow extends Window {
	private TreePanel<ModelData> treepanel;
	private ListStore<ModelData> listStore;
	private ListView<ModelData> listView;
	private TreeStore<ModelData> treeStore = new TreeStore<ModelData>();

	public DeptTreeMultiSelectWindow(Map<String, Field<String>> dept) {
		setSize(450, 500);
		setModal(true);
		setLayout(new BorderLayout());
		setHeading("选择部门");

		createWest();
		createCenter();

		addButton(getConfirmButton(dept));
		addButton(getCancelButton());
	}

	/**
	 * 显示所选中的部门
	 */
	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeading("已选部门");

		listView = new ListView<ModelData>() {
			@Override
			protected ModelData prepareData(ModelData model) {
				String name = model.get("name");
				model.set("shortName", Format.ellipse(name, 15));
				return model;
			}
		};

		listView.setStore(listStore);
		listView.setDisplayProperty("shortName");
		listView.setBorders(false);

		centerPanel.add(listView);
		add(centerPanel, centerData);
	}

	/**
	 * 创建部门树形结构
	 */
	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				225, 200, 250);
		westData.setMargins(new Margins(5, 0, 5, 5));
		VerticalPanel westPanel = new VerticalPanel();
		westPanel.setSpacing(8);
		westPanel.setBorders(true);
		westPanel.setStyleAttribute("backgroundColor", "white");

		treepanel = new TreePanel<ModelData>(treeStore);
		treepanel.setAutoLoad(true);
		treepanel.setDisplayProperty("name");
		treepanel.setCheckStyle(CheckCascade.NONE);
		treepanel.setCheckable(true);

		treepanel.addCheckListener(getListener());
		westPanel.add(treepanel);
		add(westPanel, westData);
	}

	private CheckChangedListener<ModelData> getListener() {
		listStore = new ListStore<ModelData>();
		CheckChangedListener<ModelData> listener = new CheckChangedListener<ModelData>() {
			@Override
			public void checkChanged(CheckChangedEvent<ModelData> event) {
				listStore.removeAll();
				for (int i = 0; i < event.getCheckedSelection().size(); i++) {
					listStore.add(event.getCheckedSelection().get(i));
				}
			}
		};
		return listener;
	}

	/**
	 * 取消按钮
	 * @return
	 */
	private Button getCancelButton() {
		Button button = new Button("取消");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				hide();
			}
		});
		return button;
	}

	/**
	 * 确定选取按钮
	 * @return
	 */
	private Button getConfirmButton(final Map<String, Field<String>> dept) {
		Button button = new Button("确定选取");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				StringBuffer sb = new StringBuffer();//封装部门名称
				StringBuffer sb2 = new StringBuffer();//封装部门ID
				for (ModelData dept : listStore.getModels()) {
					sb.append(", " + (String) dept.get("name"));
					sb2.append(", " + (String) dept.get("id"));
				}
				String s = sb.toString();
				String s2 = sb2.toString();
				if (s.length() > 1)
					s = s.substring(2);
				if (s2.length() > 1)
					s2 = s2.substring(2);
				//赋值
				dept.get("deptID").setValue(Format.ellipse(s2, 100));
				dept.get("deptName").setValue(Format.ellipse(s, 100));
				hide();
			}
		});
		return button;
	}

	public TreeStore<ModelData> getTreeStore() {
		return treeStore;
	}

}
